import Vue from 'vue'
import Vuex from 'vuex'
import mainContent from '@/layout'
import { menuApi, routerApi } from '@/api'
import { getRoute } from '@/utils'

Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		// 左侧菜单是否缩小化
		sideBar: false,
		// 左侧菜单
		routes: [],
		// 上部菜单标签
		routeTopNav: [
			{
				path: '/home',
				name: '首页'
			}
		]
		// 退出重置
	},
	mutations: {
		// 设置左侧菜单是否缩小化
		SET_SIDEBAR (state, val) {
			state.sideBar = val
		},
		// 设置左侧菜单
		SET_DEFAULT_ROUTES (state, routes) {
			state.routes = routes[0].children
		},
		// 清空左侧菜单
		CLEAR_DEFAULT_ROUTES (state, routes) {
			state.routes = []
		},
		// 设置上部菜单标签
		SET_routeTopNav (state, routes) {
			if (routes.length === 0) {
				state.routeTopNav = [
					{
						path: '/home',
						name: '首页'
					}
				]
			} else {
				state.routeTopNav.push(routes)
			}
		}
	},
	actions: {
		// 设置左侧菜单是否缩小化
		setSideBar ({ commit }, val) {
			commit('SET_SIDEBAR', val)
		},
		// 设置左侧菜单
		getRouter ({ commit }) {
			return new Promise(resolve => {
				getRoute().then(res => {
					commit('SET_DEFAULT_ROUTES', res)
					resolve(res)
				})
			})
		},
		// 清空左侧菜单
		clearRouter ({ commit }) {
			commit('CLEAR_DEFAULT_ROUTES', [])
		},
		// 设置上部菜单标签
		setRouteTopNav ({ commit }, val) {
			commit('SET_routeTopNav', val)
		}
	},
	getters: {
		getRoutes (state) {
			return state.routes
		}
	}
})

export default store
